package com.haidechizi.i18n.spring.boot.autoconfigure;

import com.haidechizi.i18n.common.interceptor.I18nLanguageInterceptor;
import com.haidechizi.i18n.spring.boot.properties.I18nProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableConfigurationProperties(I18nProperties.class)
@ConditionalOnWebApplication
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    I18nProperties i18nProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(i18nLanguageInterceptor()).addPathPatterns(i18nProperties.getI18nLanguageInterceptorPattern());
    }

    @Bean
    public I18nLanguageInterceptor i18nLanguageInterceptor() {
        return new I18nLanguageInterceptor();
    }
}
